#define _CRT_SECURE_NO_WARNINGS 1
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
#include<string>
using namespace std;

struct Node
{
    int val;
    Node* left;
    Node* right;
};

Node* TreeCreat()
{
    int k;
    scanf("%d", &k);
    if (k == 0)
        return NULL;
    Node* root = new Node;
    root->val = k;
    root->left = TreeCreat();
    root->right = TreeCreat();
    return root;
}

/*Node* TreeCreat(vector<int>& num, int* pi)
{
    if (num[*pi] == 0)
    {
        (*pi)++;
        return nullptr;
    }
    Node* newnode = new Node;
    newnode->val = num[*pi];
    (*pi)++;
    newnode->left = TreeCreat(num, pi);
    newnode->right = TreeCreat(num, pi);
    return newnode;
}*/

Node* Find(Node* root, int num)
{
    if (root == nullptr || root->val == num)
        return nullptr;
    if (root->right != nullptr && root->right->val == num)
        return root;
    if (root->left != nullptr && root->left->val == num)
        return root;
    Node* root1 = Find(root->left, num);
    if (root1)
        return root1;
    return Find(root->right, num);
}

int main()
{
    /*vector<int> num;
    int number;
    while (cin >> number)
    {
        num.push_back(number);
        if (cin.get() == '\n')
            break;
    }*/
    int i = 0;
    Node* root = TreeCreat();

    int n;
    cin >> n;
    while (n--)
    {
        int m;
        cin >> m;
        Node* find = Find(root, m);
        if (find == nullptr)
        {
            cout << 0 << endl;
            continue;
        }
        cout << find->val << endl;
    }
    return 0;
}